package com.lovo.movie.service;

import com.lovo.dto.OrderDto;
import com.lovo.movie.dao.IOrderDao;
import com.lovo.movie.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class OrderServiceMQ {
    @Autowired
    IOrderDao orderDao;
    @RabbitListener(queues = "orderQueue")
    public void order(OrderDto orderDto, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {

         if(null!=orderDto){
             OrderEntity order=new OrderEntity();
             BeanUtils.copyProperties(orderDto,order);
             orderDao.save(order);
             //手动确认  false不批量处理
             channel.basicAck(tag,false);
         }
    }
}
